package me.jessyan.mvparms.demo.mvp.model.logic.api;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;


import com.aispeech.tvui.kit.dsk.AIObject;
import com.aispeech.tvui.kit.dsk.duiwidget.TextWidget;
import com.aispeech.tvui.kit.dsk.listener.CommandObserver;
import com.aispeech.tvui.kit.dsk.listener.MessageObserver;
import com.aispeech.tvui.kit.dsk.listener.NativeApiObserver;



import java.util.ArrayList;

import me.jessyan.mvparms.demo.mvp.ui.xunfeisdk.xunfei.XunFeiSpeechDialogWrap;

/**
 * 主动请求音乐信息
 */
//public static final String RPC_CLIENT_REQUEST_MUSIC_INFO = "rpc.client.request.music.info";


/**
 * desc: Dsk技能辅助类
 *
 * @author Robert
 * created at 2019-06-27
 */
public class DskSkillHelper {
    private static final String TAG = DskSkillHelper.class.getSimpleName();

    /**
     * native api  -- 查询当前播放的故事名称，说法举例：“这个故事叫什么名字”
     */
    private String NATIVE_API_STORY = "audio.info.story";

    /**
     * command -- 上一集，说法举例：“上一集”
     */
    private String COMMAND_MEDIACONTROL_PREVEPISODE = "mediacontrol.x.prevepisode";

    /**
     * message -- 接收识别结果
     */
    private String INTERNAL_CONTEXT_INPUT_TEXT = "context.input.text";
    /**
     * 音乐状态通知更新
     */
    public static final String RPC_MUSIC_UPDATE_NOTIFY = "rpc.server.music.update";
    /**
     * 音乐控制
     */
    public static final String RPC_CLIENT_CONTROL_NOTIFY = "rpc.client.control";
    /**
     * 音乐进度
     */
    public static final String RPC_MUSIC_PROCESS_NOTIFY = "rpc.server.music.process";

    //主动请求音乐信息
    public static final String RPC_CLIENT_REQUEST_MUSIC_INFO = "rpc.client.request.music.info";

    public static  String rpc_client_stop_dialog = "rpc.client.stop.dialog";

    public static  String sys_dialog_start = "sys.dialog.start";//对话开始
    public static  String sys_dialog_error = "sys.dialog.error";//对话出现错误
    public static  String sys_dialog_continue = "sys.dialog.continue";//对话恢复
    public static  String sys_dialog_end = "sys.dialog.end";//对话结束
    public static  String sys_wakeup_result = "sys.wakeup.result";//语音唤醒

    private static final DskSkillHelper INSTANCE = new DskSkillHelper();
    XunFeiSpeechDialogWrap.VoiceViewListener voiceListener;
    private DskSkillHelper() {

    }

    public static DskSkillHelper getInstance() {
        return INSTANCE;
    }

    /**
     * 可以自己维护一个单例 AIObject
     */
    private AIObject mObject = new AIObject(TAG);

    /**
     * 注册command 和 nativeApi
     */
    public void register(Context mContext,XunFeiSpeechDialogWrap.VoiceViewListener voiceListener) {
        this.voiceListener = voiceListener;
        // nativeApi
        registerNativeApi(mObject);
        // command
        registerCommand(mObject);
        // message
        registerMessage(mContext,mObject);

    }

    /**
     * 取消注册
     */
    public void unRegister() {
        // mObject.destroy(); 包含以下三个。
        mObject.clearNativeApiObserver();
        mObject.clearCommandObserver();
        mObject.clearMessageObserver();
    }

    /**
     * 注册nativeApi
     *
     * @param object object
     */
    private void registerNativeApi(final AIObject object) {
        ArrayList<String> nativeApiStrList = new ArrayList<>();
        // 添加 native api
        nativeApiStrList.add(NATIVE_API_STORY);
        NativeApiObserver nativeApiObserver = new NativeApiObserver() {
            @Override
            public void onQuery(String nativeApi, String jsonStr) {
                Log.d(TAG, "[" + nativeApi + "]:" + jsonStr);

                if (NATIVE_API_STORY.equals(nativeApi)) {
                    TextWidget textWidget = new TextWidget();
                    String songName = "望梅止渴，native api测试";
                    textWidget.addExtra("storytitle", songName);
                    mObject.feedNativeApiResult(NATIVE_API_STORY, textWidget);
                }
            }
        };
        // 设置 native api list
        object.setNativeApiObserver(nativeApiStrList, nativeApiObserver);
    }


    public void publishStick(String topic, String data) {
        mObject.publishSticky(topic, data);
    }


    /**
     * 注册command
     *
     * @param object object
     */
    private void registerCommand(AIObject object) {
        ArrayList<String> commandApiStrList = new ArrayList<>();
        // 添加 command
        commandApiStrList.add(COMMAND_MEDIACONTROL_PREVEPISODE);
        CommandObserver commandObserver = new CommandObserver() {
            @Override
            public void onCall(final String command, final String jsonStr) {
                Log.d(TAG, "[" + command + "]:" + jsonStr);

                if (COMMAND_MEDIACONTROL_PREVEPISODE.equals(command)) {
                    // 写自己的播控逻辑
                }
            }
        };
        // set command list
        object.setCommandObserver(commandApiStrList, commandObserver);
    }


    /**
     * 注册message
     *
     * @param object object
     */
    private void registerMessage(Context mContext, AIObject object) {

        ArrayList<String> messageApiStrList = new ArrayList<>();
        // 添加 message
        messageApiStrList.add(INTERNAL_CONTEXT_INPUT_TEXT);
        messageApiStrList.add(RPC_MUSIC_UPDATE_NOTIFY);
        messageApiStrList.add(RPC_MUSIC_PROCESS_NOTIFY);
        messageApiStrList.add(sys_dialog_start);
        messageApiStrList.add(sys_dialog_end);
        messageApiStrList.add(sys_dialog_continue);
        MessageObserver messageObserver = new MessageObserver() {
            @Override
            public void onMessage(String message, String jsonStr) {
                if (INTERNAL_CONTEXT_INPUT_TEXT.equals(message)) {//此处处理语音识别结果
                    Log.v("语音实时识别结果",jsonStr);
                    voiceListener.notifyViewChange(jsonStr);
//                    {"type":"major","eof":1,"text":"你好小南"}
//                    {"sessionId":"3e6d4ab5bb7fbd92222714132d4b4782","recordId":"5135fa4d7253fb749ed72844d81b0309","eof":0,"var":""}
//                    {"sessionId":"3e6d4ab5bb7fbd92222714132d4b4782","recordId":"5135fa4d7253fb749ed72844d81b0309","eof":0,"var":"我想"}
//                    {"sessionId":"3e6d4ab5bb7fbd92222714132d4b4782","recordId":"5135fa4d7253fb749ed72844d81b0309","eof":0,"var":"我想听"}
//                    {"sessionId":"3e6d4ab5bb7fbd92222714132d4b4782","recordId":"5135fa4d7253fb749ed72844d81b0309","eof":0,"var":"我想听张学"}
//                    {"sessionId":"3e6d4ab5bb7fbd92222714132d4b4782","recordId":"5135fa4d7253fb749ed72844d81b0309","eof":0,"var":"我想听张学友"}
//                    {"sessionId":"3e6d4ab5bb7fbd92222714132d4b4782","recordId":"5135fa4d7253fb749ed72844d81b0309","eof":0,"var":"我想听张学友的歌"}
//                    {"pinyin":"wo xiang ting zhang xue you de ge","recordId":"5135fa4d7253fb749ed72844d81b0309","eof":1,"sessionId":"3e6d4ab5bb7fbd92222714132d4b4782","text":"我想听张学友的歌"}

                    //此处写语音识别逻辑,只要识别结果,取最后一条

                    //语音拦截
//                    if(bean.getPinyin() != null && bean.getText() != null){
//
//                        if(bean.getText().contains("天气怎么样")){
//                            //打断会话,不打断会继续执行内置语音技能
//                            DskSkillHelper.getInstance().publishStick(DskSkillHelper.rpc_client_stop_dialog, null);
//
//
//                            //处理自己的语音匹配逻辑
//                            //TO DO ...
//                        }
//                    }
                }

                if (sys_dialog_start.equals(message)) {//自定义语音识别动画,可以在此处拉起动画
                    Log.v("语音识别开始",jsonStr);
                    Toast.makeText(mContext,"语音识别开始",Toast.LENGTH_SHORT).show();
                }

                if (sys_dialog_end.equals(message)) {//自定义语音识别动画,可以在此处隐藏动画
                    Log.v("语音识别结束",jsonStr);
                    Toast.makeText(mContext,"语音识别结束",Toast.LENGTH_SHORT).show();
                }

                if (sys_dialog_continue.equals(message)) {
                    Log.v("语音识别继续",jsonStr);
                }


                if (RPC_MUSIC_UPDATE_NOTIFY.equals(message)) {
                    Log.v("歌曲信息变化",jsonStr);
                }

                if (RPC_MUSIC_PROCESS_NOTIFY.equals(message)) {
                    Log.v("歌曲播放进度",jsonStr);
                }
            }
        };
        // set message list
        object.setMessageObserver(messageApiStrList, messageObserver);
    }

}